package com.example.myapplication.ui.task;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

import com.example.myapplication.R;
import com.example.myapplication.bean.Task;
import com.example.myapplication.service.TaskService;
import com.example.myapplication.ui.component.PageEnabledSlidingPaneLayout;

import java.util.List;

public class TaskCheckActivity extends AppCompatActivity {
    private static final String TAG = "TaskCheckActivity";
    private LinearLayout taskListGroup;
    private TextView asideTaskIdTextView;//侧边栏任务编号文本框
    private TextView publishDataTextView;//侧边栏发布日期文本框
    private PageEnabledSlidingPaneLayout slidingPaneLayout;//侧滑控件
    private List<Task> taskList;//作业指令列表
    private TaskService taskService;
    private TextView taskPublishDataTextVIew;//作业指令发布日期文本框
    private TextView taskClassTextView;//作业班次文本框
    private TextView taskIdTextView;
    private TextView tallyClerkIdTextView;
    private TextView publisherIdTextView;
    private TextView contentTextArea;
    private TextView remarkTextArea;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_task_check);
        slidingPaneLayout = findViewById(R.id.sliding_pane_layout_task_check_page);
        initSlidingPaneLayout();
        taskListGroup = slidingPaneLayout.findViewById(R.id.task_list_group);
        userName=getIntent().getStringExtra("name");
        Log.d(TAG, "onCreate: "+userName);
        loadAllElement();
        initTaskList();
        initTaskMenuItem();
        if (taskList.size()!=0){
            refreshTaskCheckView(taskList.get(0));
        }


    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initSlidingPaneLayout() {
        // 设置侧划后首页上透明，不带颜色，不会变灰色
        slidingPaneLayout.setSliderFadeColor(Color.TRANSPARENT);
        // 拿到左侧菜单栏view
        final View leftView = slidingPaneLayout.getChildAt(0);
        slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(@NonNull View panel, float slideOffset) {
                // 划动过程中
                // 左侧边栏的变化
                leftView.setPivotX(-leftView.getWidth() / 5.0f);
                leftView.setPivotY(leftView.getHeight() / 2.0f);
                leftView.setScaleX(0.8f + 0.2f * slideOffset);//0.8~1
                leftView.setScaleY(0.8f + 0.2f * slideOffset);//0.8~1
                leftView.setAlpha(0.8f + 0.2f * slideOffset);//0.8~1
                // 首页的变化
                panel.findViewById(R.id.task_ll_main).setScaleX(1f - 0.2f * slideOffset);
                panel.findViewById(R.id.task_ll_main).setScaleY(1f - 0.2f * slideOffset);
                // 背景阴影图片的变化
                panel.findViewById(R.id.task_img_shadow).setScaleX(1f - 0.1f * slideOffset);
                panel.findViewById(R.id.task_img_shadow).setScaleY(1f - 0.13f * slideOffset);
                panel.findViewById(R.id.task_img_shadow).setAlpha(1f - 0.3f * slideOffset);
            }

            @Override
            public void onPanelOpened(@NonNull View view) {
            }

            @Override
            public void onPanelClosed(@NonNull View panel) {
            }
        });
    }

    private void initTaskMenuItem() {

        taskListGroup.removeAllViews();
        for (int i = 0; i < taskList.size(); i++) {
            Task task=new Task();
            task=taskList.get(i);
            LayoutInflater myInflater = LayoutInflater.from(this);
            LinearLayout taskItem;
            taskItem = (LinearLayout) myInflater.inflate(R.layout.menu_task_item, taskListGroup, false);
            taskItem.setId(task.getTid());
            taskListGroup.addView(taskItem);
            asideTaskIdTextView = taskItem.findViewById(R.id.aside_task_id_text_view);
            publishDataTextView = taskItem.findViewById(R.id.aside_publish_data_text_view);
            asideTaskIdTextView.setText(String.valueOf(task.getTid()));
            publishDataTextView.setText(task.getDates());
            Log.d(TAG, "initTaskMenuItem: " + taskItem);
            taskItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: "+taskItem.getId());
                     refreshTaskCheckView(searchTaskItem(taskItem.getId()));
                }
            });
        }
    }

    private void initTaskList(){
        taskService=new TaskService();
        taskList=taskService.getTasks(userName);
    }

    private void refreshTaskCheckView(Task task){
        if (task!=null){
            taskPublishDataTextVIew.setText(task.getDates());
            taskClassTextView.setText(task.getClasses());
            taskIdTextView.setText(String.valueOf(task.getTid()));
            tallyClerkIdTextView.setText(task.getName());
            publisherIdTextView.setText(task.getPublisher());
            contentTextArea.setText(task.getContent());
            remarkTextArea.setText(task.getRemark());
        }

    }

    @SuppressLint("WrongViewCast")
    public void loadAllElement(){
        taskPublishDataTextVIew=findViewById(R.id.task_publish_data_text_view);
        taskClassTextView=findViewById(R.id.task_class_text_view);
        taskIdTextView=findViewById(R.id.task_id_text_view);
        tallyClerkIdTextView=findViewById(R.id.tally_clerk_id_text_view);
        publisherIdTextView=findViewById(R.id.publisherId_text_view);
        contentTextArea=findViewById(R.id.content_text_area);
        remarkTextArea=findViewById(R.id.remark_text_area);
    }

    public Task searchTaskItem(Integer taskId){
        for (Task task : taskList){
            if (task.getTid()==taskId){
                return task;
            }
        }
        return null;
    }
}

